package com.example.demo.itemReader.itemReadeFromDatabase;


import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.RowMapper;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableBatchProcessing
public class ItemReaderDatabaseDemo {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Resource
    private DataSource dataSource;
    @Resource
    @Qualifier("databaseJDBCWriter")
    private ItemWriter<? super User> databaseJDBCWriter;
    @Bean
    public Job itemReaderDatabaseDemoJob1(){
        return jobBuilderFactory.get("itemReaderDatabaseDemoJob1")
                .start(itemReaderDatabaseDemoStep01())
                .build();
    }

    @Bean
    public Step itemReaderDatabaseDemoStep01() {
        return stepBuilderFactory.get("itemReaderDatabaseDemoStep01")
                .<User,User>chunk(2)
                .reader(databaseJDBCReader())
                .writer(databaseJDBCWriter)
                .build();
    }

    @Bean
    @StepScope//范围只限于step范围之内
    public JdbcPagingItemReader<User> databaseJDBCReader() {
        JdbcPagingItemReader<User> reader = new JdbcPagingItemReader<User>();
        reader.setDataSource(dataSource);//指定数据源
        reader.setFetchSize(2);//一次读取几条
        //把读取到的就转化为User对象
        reader.setRowMapper(new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getInt(1));
                user.setUserName(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setAge(rs.getInt(4));
                return user;
            }
        });

        //指定sql语句
        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        provider.setSelectClause("id,userName,password,age");
        provider.setFromClause("from user");

        //指定根据哪个字段排序
        Map<String, Order> sort = new HashMap<>();
        sort.put("id",Order.ASCENDING);
        provider.setSortKeys(sort);

        reader.setQueryProvider(provider);
        return reader;
    }


}
